
package com.iris.live.services.report.sources;

import com.iris.live.services.data.repositories.ReportRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by lusong on 2016/12/12.
 */
@Component("LSHROSWeekReportSource")
public class LSHROSWeekReportSource extends AbstractReportSourceService {
    private static final Logger logger = LoggerFactory.getLogger(LSHROSWeekReportSource.class);
    private static final String[] lastType = {"-CUSTOMER_ORDER_WEEKLY", "-CUSTOMER_ORDER_MTD", "-CUSTOMER_ORDER_YTD", "-CUSTOMER_ORDER_TOTAL_UNFULFILLED",
            "-RETAIL_SALE_WEEKLY", "-RETAIL_SALE_MTD", "-RETAIL_SALE_YTD",
            "-STOCK_WITH_ORDER", "-STOCK_WITHOUT_ORDER", "-STOCK_PENDING_FOR_INVOICE",
            "",
            "-oneJul", "-oneAug", "-oneSep", "-oneOct", "-oneNov", "-oneDec",
            "-tweJan", "-tweFeb", "-tweMar", "-tweApr", "-tweMay", "-tweJun", "-tweJul", "-tweAug", "-tweSep", "-tweOct", "-tweNov", "-tweDec",
            "-threeJan", "-threeFeb", "-threeMar", "-threeApr", "-threeMay", "-threeJun", "-threeJul", "-threeAug", "-threeSep", "-threeOct", "-threeNov", "-threeDec",
            "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "-WHOLESALE_NOTINTO_STORAGE", "-RETAIL_SALE_WITHOUT_GIVECAR"};

    private static final String[] indicators = {"Volume Pillar-C-W205-C200", "Volume Pillar-C-W205-C200 4MATIC", "Volume Pillar-C-W205-C300",
            "Volume Pillar-C-V205-C180 L", "Volume Pillar-C-V205-C200 L", "Volume Pillar-C-V205-C200 L 4MATIC", "Volume Pillar-C-V205-C260 L", "Volume Pillar-C-V205-C300 L", "Volume Pillar-C-V205-C350 e L",
            "formular",
            "Volume Pillar-E-V213-E200 L", "Volume Pillar-E-V213-E300 L",
            "Volume Pillar-E-V212-E180 L 2nd FL", "Volume Pillar-E-V212-E200 L 2nd FL", "Volume Pillar-E-V212-E260 L 2nd FL", "Volume Pillar-E-V212-E320 L 2nd FL", "Volume Pillar-E-V212-E400 L 2nd FL", "Volume Pillar-E-V212-E260 L FL", "Volume Pillar-E-V212-E300 L FL", "Volume Pillar-E-V212-E400 L FL", "Volume Pillar-E-V212-E300 L",
            "formular",
            "Volume Pillar-S-V222-S320 L", "Volume Pillar-S-V222-S400 L", "Volume Pillar-S-V222-S400 L 4MATIC", "Volume Pillar-S-V222-S400 L Hybrid", "Volume Pillar-S-V222-S500 e L", "Volume Pillar-S-V222-S500 L", "Volume Pillar-S-V222-S500 4MATIC", "Volume Pillar-S-V222-S600 L",
            "Volume Pillar-S-V221-S350 L 4MATIC", "Volume Pillar-S-V221-S400 L Hybrid", "Volume Pillar-S-V221-S600 L",
            "formular",
            "formular",
            "SUV-GLA-X156-GLA 200", "SUV-GLA-X156-GLA 220 4MATIC", "SUV-GLA-X156-GLA 260 4MATIC",
            "formular",
            "SUV-GLA(CBU)-X156-GLA 200", "SUV-GLA(CBU)-X156-GLA 260",
            "formular",
            "SUV-GLC-X253-GLC 200 4MATIC", "SUV-GLC-X253-GLC 260 4MATIC", "SUV-GLC-X253-GLC 300 4MATIC",
            "formular",
            "SUV-GLK-X204-GLK 200", "SUV-GLK-X204-GLK 260", "SUV-GLK-X204-GLK 300",
            "formular",
            "SUV-GLE-W166-GLE 300 4MATIC", "SUV-GLE-W166-GLE 320 4MATIC", "SUV-GLE-W166-GLE 350 4MATIC", "SUV-GLE-W166-GLE 400 4MATIC", "SUV-GLE-W166-GLE 450 4MATIC", "SUV-GLE-W166-GLE 500 e 4MATIC",
            "formular",
            "SUV-M-W166-ML320", "SUV-M-W166-ML350 CDI", "SUV-M-W166-ML400", "SUV-M-W166-ML350",
            "formular",
            "SUV-GLE Sport-C292-GLE 320 4MATIC Sport", "SUV-GLE Sport-C292-GLE 400 4MATIC Sport", "SUV-GLE Sport-C292-GLE 450 AMG 4MATIC Sport",
            "formular",
            "SUV-GLS-X166-GLS 350 4MATIC", "SUV-GLS-X166-GLS 400 4MATIC", "SUV-GLS-X166-GLS 500 4MATIC",
            "formular",
            "SUV-GL-X166-GL350 CDI 4MATIC", "SUV-GL-X166-GL400 4MATIC", "SUV-GL-X166-GL500 4MATIC", "",
            "formular",
            "SUV-G-W463-G500",
            "formular",
            "formular",
            "Compact Car-A-W176-A180", "Compact Car-A-W176-A200", "Compact Car-A-W176-A260",
            "formular",
            "Compact Car-B-W246-B180", "Compact Car-B-W246-B200", "Compact Car-B-W246-B260",
            "formular",
            "Compact Car-CLA-C117-CLA200", "Compact Car-CLA-C117-CLA220", "Compact Car-CLA-C117-CLA260",
            "formular",
            "formular",
            "Dream Car-C Estate-S205-C180 Estate", "Dream Car-C Estate-S205-C200 4MATIC Estate", "Dream Car-C Estate-S205-C300 Estate", "Dream Car-C Estate-S205-C200 Estate", "Dream Car-C Estate-S204-C200 Estate",
            "formular",
            "Dream Car-C Coupe-C205-C200 Coupe", "Dream Car-C Coupe-C205-C300 Coupe", "Dream Car-C Coupe-C204-C180 Coupe",
            "formular",
            "Dream Car-SLC-R172-SLC200", "Dream Car-SLC-R172-SLC300",
            "formular",
            "Dream Car-SLK-R172-SLK200", "Dream Car-SLK-R172-SLK350",
            "formular",
            "Dream Car-E Coupe-C207-E200 Coupe", "Dream Car-E Coupe-C207-E260 Coupe", "Dream Car-E Coupe-C207-E320 Coupe", "Dream Car-E Coupe-C207-E350 Coupe", "Dream Car-E Coupe-C207-E400 Coupe",
            "formular",
            "Dream Car-E Cabrio-A207-E260 Cabriolet", "Dream Car-E Cabrio-A207-E350 Cabriolet", "Dream Car-E Cabrio-A207-E400 Cabriolet", "Dream Car-E Cabrio-A207-E260 Cabriolet FL", "Dream Car-E Cabrio-A207-E400 Cabriolet FL",
            "formular",
            "Dream Car-CLS-C218-CLS260", "Dream Car-CLS-C218-CLS320", "Dream Car-CLS-C218-CLS400 4MATIC", "Dream Car-CLS-C218-CLS300", "Dream Car-CLS-C218-CLS350", "Dream Car-CLS BE-X218-CLS260 BE", "Dream Car-CLS BE-X218-CLS350 BE", "Dream Car-CLS BE-X218-CLS400 BE",
            "formular",
            "Dream Car-SL-R230-SL300", "Dream Car-SL-R231-SL350", "Dream Car-SL-R231-SL400",
            "formular",
            "Dream Car-S Coupe-C217-S400 4MATIC Coupe", "Dream Car-S Coupe-C217-S500 4MATIC Coupe",
            "formular",
            "Dream Car-CL-C216-CL600",
            "formular",
            "Dream Car-R-V251-R320", "Dream Car-R-V251-R400", "Dream Car-R-V251-R300", "Dream Car-R-V251-R350", "Dream Car-R-V251-R500",
            "formular",
            "formular",
            "MM-S-X222-MM S400", "MM-S-X222-MM S500", "MM-S-X222-MM S600",
            "formular",
            "AMG-A45-W176-A45", "AMG-GLA45-X156-GLA45", "AMG-CLA45-C117-CLA45", "AMG-C63-W205-C63", "AMG-C63 Dynamic-W204-C63 Dynamic", "AMG-C63 Sedan-W204-C63 Sedan",
            "AMG-C63 Coupe-W204-C63 Coupe", "AMG-C63 Coupe-C205-C63 Coupe", "AMG-C63 Coupe-C204-C63 Coupe", "AMG-SLK55-R171-SLK55", "AMG-AMG GT-C190-AMG GT", "AMG-AMG GT S-C190-AMG GT S",
            "AMG-CLS63-C218-CLS63", "AMG-GLE63 Sport-C292-GLE63 Sport", "AMG-S63 Coupe-C217-S63 Coupe", "AMG-S63-V222-S63", "AMG-S65-V222-S65", "AMG-S65-V221-S65",
            "AMG-SL63-R231-SL63", "AMG-SLS-C197-SLS", "AMG-G63-W463-G63", "AMG-G65-W463-G65", "AMG-GLE63-W166-GLE63", "AMG-ML63-W166-ML63", "AMG-GLS63-X166-GLS63", "AMG-GL63-X166-GL63",
            "formular", "formular", "formular", "formular", "formular",
            "", "", "", "",
            "formular", "formular"};

    @Autowired
    private ReportRepository reportRepository;

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) {
        String date;
        if(context.containsKey("date")){
            date =context.get("date").toString();
        }else{
            Date now=new Date();
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
            date = sdf2.format(now);
        }

        Map values = getDataAboutWeekReport(date,sheet.getCode());
        List<Row> rows = new ArrayList<>();

        rows.add(new Row());
        rows.add(new Row());

        Cell[] cells = new Cell[1];
        cells[0] = new Cell(sheet.getName(), 7);
        rows.add(new Row(cells));

        //rows.add(new Row());
        rows.add(new Row());
        rows.add(new Row());
        rows.add(new Row());
        rows.add(new Row());
        for (String indicator : indicators) {
            if (indicator == null || "".equals(indicator)) {
                rows.add(new Row());
            } else if (indicator.equals("formular")) {
                rows.add(new Row(true));
            } else {
                rows.add(new Row(getCells(values, indicator)));
            }
        }
        return rows;
    }

    private Cell[] getCells(Map values, String indicator) {
        Cell[] cells = new Cell[lastType.length];
        String value = null;
        for (int index = 0; index < lastType.length; index++) {
            if (!"".equals(lastType[index])) {
                Object o = values.get(indicator + lastType[index]);
                value = (o == null ? "0" : o).toString();
            } else {
                Object o1 = values.get(indicator + lastType[index - 2]);
                Object o2 = values.get(indicator + lastType[index - 1]);
                value = Double.parseDouble((o1 == null ? "0" : o1).toString()) + Double.parseDouble((o2 == null ? "0" : o2).toString()) + "";
            }
            cells[index] = new Cell(value, "double", 5 + index);
        }
        return cells;
    }

    private Map getDataAboutWeekReport(String date,String type) {
        Map map = new HashMap();
        try {
            List list = null;
            String[] types = type.split(",");
            if (types[0].equals("one")) {
                list = reportRepository.getWeekReportOne(types[1],date);
            } else {
                list = reportRepository.getWeekReport(type,date);
            }


            for (Object o : list) {
                Map m = (Map) o;
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-CUSTOMER_ORDER_WEEKLY", m.get("CUSTOMER_ORDER_WEEKLY"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-CUSTOMER_ORDER_MTD", m.get("CUSTOMER_ORDER_MTD"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-CUSTOMER_ORDER_YTD", m.get("CUSTOMER_ORDER_YTD"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-CUSTOMER_ORDER_TOTAL_UNFULFILLED", m.get("CUSTOMER_ORDER_TOTAL_UNFULFILLED"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-RETAIL_SALE_WEEKLY", m.get("RETAIL_SALE_WEEKLY"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-RETAIL_SALE_MTD", m.get("RETAIL_SALE_MTD"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-RETAIL_SALE_YTD", m.get("RETAIL_SALE_YTD"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-STOCK_WITH_ORDER", m.get("STOCK_WITH_ORDER"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-STOCK_WITHOUT_ORDER", m.get("STOCK_WITHOUT_ORDER"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-STOCK_PENDING_FOR_INVOICE", m.get("STOCK_PENDING_FOR_INVOICE"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-ORDER_REQUIREMENT_PLAN", m.get("ORDER_REQUIREMENT_PLAN"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneJan", m.get("oneJan"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneFeb", m.get("oneFeb"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneMar", m.get("oneMar"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneApr", m.get("oneApr"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneMay", m.get("oneMay"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneJun", m.get("oneJun"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneJul", m.get("oneJul"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneAug", m.get("oneAug"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneSep", m.get("oneSep"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneOct", m.get("oneOct"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneNov", m.get("oneNov"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneDec", m.get("oneDec"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweJan", m.get("tweJan"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweFeb", m.get("tweFeb"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweMar", m.get("tweMar"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweApr", m.get("tweApr"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweMay", m.get("tweMay"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweJun", m.get("tweJun"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweJul", m.get("tweJul"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweAug", m.get("tweAug"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweSep", m.get("tweSep"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweOct", m.get("tweOct"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweNov", m.get("tweNov"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweDec", m.get("tweDec"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeJan", m.get("threeJan"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeFeb", m.get("threeFeb"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeMar", m.get("threeMar"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeApr", m.get("threeApr"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeMay", m.get("threeMay"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeJun", m.get("threeJun"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeJul", m.get("threeJul"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeAug", m.get("threeAug"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeSep", m.get("threeSep"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeOct", m.get("threeOct"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeNov", m.get("threeNov"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeDec", m.get("threeDec"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-WHOLESALE_NOTINTO_STORAGE", m.get("WHOLESALE_NOTINTO_STORAGE"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-RETAIL_SALE_WITHOUT_GIVECAR", m.get("RETAIL_SALE_WITHOUT_GIVECAR"));

            }
        } catch (Exception e) {
            logger.error("周报表数据查询异常", e);
        }
        return map;
    }
}

